package com.xue.controller;

import com.xue.entity.Community;
import com.xue.entity.User;
import com.xue.maper.CommunityRepository;
import com.xue.maper.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/community")
public class CommunityController {


    @Autowired
    CommunityRepository communityRepository;

    @Autowired
    UserRepository userRepository;


    @PostMapping
    public void create(String name) {
        Community build = Community.builder().name(name).build();
        communityRepository.save(build);
    }

    @PostMapping("/user")
    public void user(String name) {
        User build = User.builder().name(name).build();
        userRepository.save(build);
    }

    @PostMapping("/relation")
    public void createRelation(String username, String communityname) {
        communityRepository.createRelation(username, communityname);
    }

    @GetMapping("/communityOfUserCount")
    public List<Map<String, Object>> communityOfUserCount() {
        return communityRepository.communityOfUserCount();
    }

}
